package com.zx.service.Impl;/**
 * Author:JasonZ
 * Data:2023/4/18
 */

import com.zx.common.TableId;
import com.zx.context.AccessContext;
import com.zx.dao.ECommerceAddressDao;
import com.zx.entity.EcommerceAddress;
import com.zx.model.vo.LoginUserInfo;
import com.zx.service.IECommerceAddressService;
import com.zx.vo.AddressInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: ecommerce-parent
 * @description:
 * @author: JasonZ
 * @create: 2023-04-18 13:59
 **/
@Service
@Slf4j
public class ECommerceAddressServiceImpl implements IECommerceAddressService {

    @Resource
    private ECommerceAddressDao addressDao;

    /**
     * 创建用户地址信息
     *
     * @param addressInfo
     * @return
     */
    @Override
    public TableId creatAddressInfo(AddressInfo addressInfo) {
        //先获取当前用户ID
        Long currentUserId = AccessContext.getUserInfo().getId();
        //把addressInfo 转化为实体
        List<EcommerceAddress> ecommerceAddresses = addressInfo.getAddressItems().stream().map(
                (addressItem -> EcommerceAddress.to(currentUserId, addressItem))
        ).collect(Collectors.toList());
        //把新增的地址信息存入到数据库
        List<EcommerceAddress> addresses = addressDao.saveAll(ecommerceAddresses);
        //把保存成功的地址信息中的地址id取出来
        List<Long> addressIds = addresses.stream().map(EcommerceAddress::getId).collect(Collectors.toList());
        //把addressIds封装成TableID传给前台
        return new TableId(addressIds.stream().map(TableId.Id::new).collect(Collectors.toList()));
    }

    /**
     * 获取当前用户地址信息
     *
     * @return
     */
    @Override
    public AddressInfo getCurrentAddressInfoByCurrentUserId() {
        //获取当前用户
        Long currentUserId = AccessContext.getUserInfo().getId();
        //查询用户地址信息
        List<EcommerceAddress> addresses = addressDao.findAllByUserId(currentUserId);
        //返回地址信息
        return new AddressInfo(
                currentUserId,
                addresses.stream().map(EcommerceAddress::toAddressItem).collect(Collectors.toList())
        );
    }

    /**
     * 根据ID获取地址信息
     *
     * @param id
     * @return
     */
    @Override
    public AddressInfo getAddressInfoById(Long id) {
        return new AddressInfo(
                AccessContext.getUserInfo().getId(),
                addressDao.findAllById(id).stream().map(EcommerceAddress::toAddressItem).collect(Collectors.toList())
        );
    }

    /**
     * 根据多个id获取地址信息
     *
     * @param id
     * @return
     */
    @Override
    public AddressInfo getAddressInfoByTableId(TableId id) {
        //1.获取到id对应的所有的地址信息
        List<TableId.Id> ids = id.getIds();
        List<EcommerceAddress> addressList = addressDao.findAllById(ids.stream().map(TableId.Id::getId).collect(Collectors.toList()));
        //2.遍历addressList 把其中的EcommerceAddress转成AddressItem
        List<AddressInfo.AddressItem> addressItems = addressList.stream()
                .map(EcommerceAddress::toAddressItem).collect(Collectors.toList());
        return new AddressInfo(
                AccessContext.getUserInfo().getId(),
                addressItems
        );
    }
}
